/**
 *
 * ......................我佛慈悲.......................
 *                       _oo0oo_
 *                      o8888888o
 *                      88" . "88
 *                      (| -_- |)
 *                      0\  =  /0
 *                    ___/`---'\___
 *                  .' \\|     |// '.
 *                 / \\|||  :  |||// \
 *                / _||||| -卍-|||||- \
 *               |   | \\\  -  /// |   |
 *               | \_|  ''\---/''  |_/ |
 *               \  .-\__  '-'  ___/-. /
 *             ___'. .'  /--.--\  `. .'___
 *          ."" '<  `.___\_<|>_/___.' >' "".
 *         | | :  `- \`.;`\ _ /`;.`/ - ` : | |
 *         \  \ `_.   \_ __\ /__ _/   .-` /  /
 *     =====`-.____`.___ \_____/___.-`___.-'=====
 *                       `=---='
 *
 *..................佛祖开光 ,永无BUG....................
 *         CSDN博客:http://blog.csdn.net/song5347
 *       github地址:https://github.com/lingsuiderongyan
 *             邮箱:lingsuiderongyan@163.com
 */

import UIKit
import SnapKit

//子控件之间的间距
let StatusTableViewCellMargin: CGFloat = 10
//微博首页字体大小
let StatusFontSize: CGFloat = 14

class LSStatusTabViewCell: UITableViewCell {
    
    //toolbar底部约束记录
    var toolBarTopConstraint: Constraint?
    
    //MARK: - 处理微博数据的ViewModel
    var statusViewModel: LSStatusViewModel? {
        didSet {
            //设置数据
            originView.statusViewModel = statusViewModel
            toolBar.statusViewModel = statusViewModel
            
            print(statusViewModel?.status?.retweeted_status?.text)
            //卸载上一次的约束
            toolBarTopConstraint?.uninstall()
            
            //判断是否有转发微博对象
            if statusViewModel?.status?.retweeted_status != nil {
                //有转发微博
                //显示转发微博视图
                retWeetView.hidden = false
                
                //更新约束
                toolBar.snp_updateConstraints(closure: { (make) -> Void in
                    self.toolBarTopConstraint = make.top.equalTo(retWeetView.snp_bottom).constraint
                })
                
                //绑定转发微博数据
                retWeetView.statusViewModel = statusViewModel
                
            } else {
                //无转发微博
                //隐藏转发微博
                retWeetView.hidden = true
                //更新约束(注意末尾的.constraint,去掉的话上下拉会把转发视图刷掉)
                toolBar.snp_updateConstraints(closure: { (make) -> Void in
                    self.toolBarTopConstraint = make.top.equalTo(originView.snp_bottom).constraint
                })
            }
        }
    }
    
    //MARK: - 懒加载
    //原创微博视图
    private lazy var originView: LSStatusOriginView = {
        let view = LSStatusOriginView()
        
        return view
    }()
    
    //转发微博的视图
    private lazy var retWeetView: LSStatusRectweetView = {
        let view = LSStatusRectweetView()
        
        return view
    }()
    
    //toolbar
    private lazy var toolBar: LSStatusToolBar = LSStatusToolBar()
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        setuoUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //MARK: - 添加控件,设置约束
    private func setuoUI() {
        
        //修改contentView的背景色
        contentView.backgroundColor = UIColor(white: 0.93, alpha: 1)
        
        //添加控件
        contentView.addSubview(originView)
        contentView.addSubview(retWeetView)
        contentView.addSubview(toolBar)
        
        
        //设置约束
        originView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(contentView)
            make.leading.equalTo(contentView)
            make.trailing.equalTo(contentView)
            
        }
        
        retWeetView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(originView.snp_bottom)
            make.leading.equalTo(originView)
            make.trailing.equalTo(originView)
        }
        
        toolBar.snp_makeConstraints { (make) -> Void in
            self.toolBarTopConstraint = make.top.equalTo(retWeetView.snp_bottom).constraint
            make.leading.equalTo(contentView)
            make.trailing.equalTo(contentView)
            make.height.equalTo(35)
        }
        
        //contenVIew的底部约束 = toolbar底部约束+间距
        contentView.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(toolBar)
            make.top.equalTo(self)
            make.leading.equalTo(self)
            make.trailing.equalTo(self)
        }
    }
}













